local dianjun = fk.CreateSkill{
  name = "bd__dianjun",
}

Fk:loadTranslationTable{
  ["bd__dianjun"] = "殿军",
  [":bd__dianjun"] = "出牌阶段结束时，你可以弃置一名其他角色的一张牌，视为其对你使用【决斗】，"..
    "然后若你未因此受伤，你可以重复此流程（不能选择本回合选择过的角色）。",
}

dianjun:addEffect(fk.EventPhaseEnd, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(dianjun.name) and player == target and data.phase == Player.Play
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local available_list = table.filter(room:getOtherPlayers(player), function (p)
      return not p:isNude()
    end)
    while #available_list > 0 do
      local tos = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = available_list,
        skill_name = dianjun.name,
      })
      if #tos == 0 then break end
      local to = tos[1]
      table.removeOne(available_list, to)
      available_list = table.filter(available_list, function (p)
        return not p:isNude()
      end)
      local c = room:askToChooseCard(player, {
        min_num = 1,
        max_num = 1,
        target =  to ,
        skill_name = dianjun.name,
        flag = "he"
      })
      room:throwCard(c, dianjun.name, to, player)
      local duel = Fk:cloneCard("duel")
      duel.skillName = dianjun.name
      local use = { ---@type UseCardDataSpec
        from = to,
        tos = { player },
        card = duel,
      }
      room:useCard(use)
      local damage_event = room.logic:getEventsByRule(GameEvent.Damage, 1, function (e)
        return e.data.to == player and e.data.card and e.data.card == duel
      end)
      if #damage_event > 0 then
        break
      end
    end
  end
})

return dianjun
